package com.huiyujinrong.qiangdanbao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.activity.fragment.FragmentMy;
import com.huiyujinrong.qiangdanbao.activity.fragment.FragmentOrder;
import com.huiyujinrong.qiangdanbao.activity.fragment.HomeFragment;
import com.huiyujinrong.qiangdanbao.activity.login.LoginActivity;
import com.huiyujinrong.qiangdanbao.utils.AppManager;
import com.huiyujinrong.qiangdanbao.utils.SharedHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * fragment 管理 操作
 * 2017/04/11
 * yd
 */
public class ActivityFragment extends AppCompatActivity implements FragmentOrder.Jump {

    private HomeFragment homeFragment;
//    private FragmentFound found;
    private FragmentOrder fOrder;
    private FragmentMy fMy;


    @BindView(R.id.image_grabsingle)
    ImageView image_grabsingle;

    @BindView(R.id.image_order)
    ImageView image_order;

    @BindView(R.id.image_found)
    ImageView image_found;

    @BindView(R.id.text_found)
    TextView text_found;

    @BindView(R.id.image_my)
    ImageView image_my;

    @BindView(R.id.text_grablesingle)
    TextView text_grabsinle;

    @BindView(R.id.text_order)
    TextView text_order;

    @BindView(R.id.text_my)
    TextView text_my;


    private long exitTime = 0;
    private boolean isLogin = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        ButterKnife.bind(this);
        /**
         * 添加activity  到 任务栈
         */
        AppManager.getInstance().addActivity(this);

        inDataFragment();

        showItem(0);
        getSupportFragmentManager()
                .beginTransaction()
                .show(homeFragment)
                .commit();
    }



    @Override
    protected void onResume() {
        super.onResume();
        isLogin = (boolean) SharedHelper.get(this, "isLogin", false);
    }

    /**
     * 初始化fragment
     */
    private void inDataFragment() {
        homeFragment = new HomeFragment();
//        found = new FragmentFound();
        fOrder = new FragmentOrder();
        fMy = new FragmentMy();
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.af_fragment, homeFragment)
//                .add(R.id.af_fragment,found)
                .add(R.id.af_fragment, fOrder)
                .add(R.id.af_fragment, fMy)
                .hide(homeFragment)
//                .hide(found)
                .hide(fOrder)
                .hide(fMy)
                .commit();
    }


    /**
     * 众多点击事件
     *
     * @param view
     */
    @OnClick({R.id.af_item_grabsingle, R.id.af_item_found,R.id.af_item_order, R.id.af_item_my})
    void showFragment(View view) {
        switch (view.getId()) {
            case R.id.af_item_grabsingle:
                //抢单
                showItem(0);
                showFragment(homeFragment);
                break;
            case R.id.af_item_order:
                if (isLogin) {
                    //订单
                    showItem(1);
                    showFragment(fOrder);
                } else {
                    Intent intent = new Intent(this, LoginActivity.class);
                    intent.putExtra("isBack", true);
                    startActivityForResult(intent, 100);
                }
                break;
            case R.id.af_item_found:
                if (isLogin) {
                    //发现
                    showItem(3);
//                    showFragment(found);
                } else {
                    Intent intent = new Intent(this, LoginActivity.class);
                    intent.putExtra("isBack", true);
                    startActivityForResult(intent, 100);
                }
                break;
            case R.id.af_item_my:
                if (isLogin) {
                    //我的
                    showItem(2);
                    showFragment(fMy);
                } else {
                    Intent intent = new Intent(this, LoginActivity.class);
                    intent.putExtra("isBack", true);
                    startActivityForResult(intent, 100);
                }
                break;
        }
    }

    /**
     * 显示fragment
     *
     * @param f
     */
    private void showFragment(Fragment f) {
        if (f != null && f.isHidden()) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .hide(homeFragment)
//                    .hide(found)
                    .hide(fOrder)
                    .hide(fMy)
                    .show(f)
                    .commit();
        }
    }

    /**
     * 显示对应  图标 以及 文字  and 文字颜色
     *
     * @param number
     */
    private void showItem(int number) {

        image_grabsingle.setBackgroundResource(R.drawable.tab_rob);
        text_grabsinle.setTextColor(getResources().getColor(R.color.item_no));
        image_found.setBackgroundResource(R.drawable.tab_find);
        text_found.setTextColor(getResources().getColor(R.color.item_no));
        image_order.setBackgroundResource(R.drawable.tab_order);
        text_order.setTextColor(getResources().getColor(R.color.item_no));
        image_my.setBackgroundResource(R.drawable.tab_mine);
        text_my.setTextColor(getResources().getColor(R.color.item_no));
        switch (number) {
            case 0:
                image_grabsingle.setBackgroundResource(R.drawable.tab_rob_s);
                text_grabsinle.setTextColor(getResources().getColor(R.color.item_ys));
                break;
            case 1:
                image_order.setBackgroundResource(R.drawable.tab_order_s);
                text_order.setTextColor(getResources().getColor(R.color.item_ys));
                break;
            case 2:
                image_my.setBackgroundResource(R.drawable.tab_mine_s);
                text_my.setTextColor(getResources().getColor(R.color.item_ys));
                break;
            case 3:
                image_found.setBackgroundResource(R.drawable.tab_find_s);
                text_found.setTextColor(getResources().getColor(R.color.item_ys));
                break;
        }
    }


    /**
     * 双击 退出 666
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    /**
     * 列哈儿的功能是,订单那边没数据滴时候后点击“抢一单” ,就会跳到抢单列表头来
     */
    @Override
    public void jump() {
        showItem(0);
        showFragment(homeFragment);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if (requestCode == 100) {
                showItem(0);
                showFragment(homeFragment);
            } else if(requestCode ==0){
                showItem(1);
                showFragment(fOrder);
            }
        }
    }

}
